package cn.javadog.ss.spring.tx;


import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 余勇
 * @date 2019年11月25日 18:21:00
 */
public class JdbcDao {

	private JdbcTemplate jdbcTemplate;
	public JdbcDao(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate = jdbcTemplate;
	}

	public boolean existCityTime(String cityTime){
		List<Map<String, Object>> cities = jdbcTemplate.queryForList(String.format("SELECT * FROM t_like WHERE city = '%s'", cityTime));
		return !cities.isEmpty();
	}

	@Transactional(rollbackFor = RuntimeException.class)
	public void testTxWithException(String cityTime){
		jdbcTemplate.execute(String.format("INSERT INTO t_like(city) VALUES ('%s')", cityTime));
		throw new RuntimeException("抛出运行时错误，测试spring事务");
	}

}
